package com.runen.infinitemovement.alioss;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.OSS;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.alibaba.sdk.android.oss.model.PutObjectResult;
import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.tool.CommonUtil;


/**
 * Create by LiaoDuanHong
 * Create on 2020/06/17 09:48
 */
public class OssUploadManager {
    private Context context;
    private AliInitUtils aliInitUtils;
    private OSS oss;
    private OssUpCompletionHandler ossUpCompletionHandler;
    /**
     * 图片名字
     */
    String imgFile;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 1) {
                loadInfo();
            }
        }
    };

    private void loadInfo() {
        byte[] bytes = CommonUtil.bitmap2Bytes(BitmapFactory.decodeFile(imgPath));
        imgFile = String.valueOf(System.currentTimeMillis());
        PutObjectRequest put = new PutObjectRequest(Constant.BUCKET_NAME, key, bytes);
        // 异步上传时可以设置进度回调
        put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
            @Override
            public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {

            }
        });

        OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
            @Override
            public void onSuccess(PutObjectRequest request, PutObjectResult result) {
                final String url = oss.presignPublicObjectURL(Constant.BUCKET_NAME, key);
                ossUpCompletionHandler.complete(url, request, result);
            }

            @Override
            public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
                // 请求异常
                if (clientExcepion != null) {
                    // 本地异常如网络异常等
                    clientExcepion.printStackTrace();
                }
            }
        });

    }

    public OssUploadManager(Context context) {
        this.context = context;
    }

    private String key;
    private String imgPath;
    private String bucketName;

    public void put(String bucketName,String endpoint, String stsServer, final String key, String imgPath, final OssUpCompletionHandler ossUpCompletionHandler) {
        aliInitUtils = new AliInitUtils(endpoint, stsServer);
        Constant.BUCKET_NAME=bucketName;
        this.key = key;
        this.imgPath = imgPath;
        this.ossUpCompletionHandler = ossUpCompletionHandler;
        new Thread(new Runnable() {
            @Override
            public void run() {
                oss = aliInitUtils.initOSS(context);
                // TODO
                // 在这里进行 http request.网络请求相关操作
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        }).start();


    }
}
